/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package album;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.persistence.Query;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import radiostation.DBManager;
import static radiostation.DBManager.*;

import radiostation_POJOS.*;

/**
 *
 * @author mman
 */
public class NewOrModifyAlbumArtistForm extends javax.swing.JFrame {
    /**
     * apo mano:declarations
     */
    Album a;
    boolean mod;
    //apo mano: constructor
  
   public NewOrModifyAlbumArtistForm(Album a,boolean mod) {
        initComponents();
        this.a=a;
        this.mod=mod;
        //από μανο: γεμισμα comboboxes
       //εδώ γεμίζω το disk number: δεν είμαι σίγουρος τι ειναι αυτό
       cmbDiskNum.removeAllItems();
        for (int i=1; i<10; i++){
            cmbDiskNum.addItem(i);
        }    
       //εδώ ανοίγω connection με τη βαση για να γεμισω τα combos
        DBManager.openConnection();
        Query q;
        q = DBManager.getEm().createQuery("SELECT mp FROM Musicproductioncompany mp ", Musicproductioncompany.class);
        List result = q.getResultList();
        System.out.print(q.getResultList()); 
        if (!result.isEmpty()) { // βρήκα τη μέθοδο από το ίντερνετ
            Iterator itr=result.iterator();
            while (itr.hasNext()){
                Musicproductioncompany mp = (Musicproductioncompany)itr.next();
                cmbMusicCompany.addItem(mp);
            }
        }
        q = DBManager.getEm().createQuery("SELECT ar FROM Artist ar ", Artist.class);
        result = q.getResultList();
        if (!result.isEmpty()) { 
            Iterator itr=result.iterator();
            while (itr.hasNext()){
                Artist mg = (Artist)itr.next();
                cmbArtists.addItem(mg);
            }
        }
        if (a!=null){//αν μεταβάλεις το υπαρχον album
            txtTitle.setText(a.getTitle());
            cmbArtists.setSelectedItem(a.getFkArtistId());
            //jDateChooser1.setDate(a.getReleasedate()); 
            cmbType.setSelectedItem(a.getType());
            cmbDiskNum.setSelectedItem(a.getDisknumber());
            cmbArtists.setSelectedItem(a.getFkMusiccompanyid());
      /*      this.songs=new ArrayList();//(ArrayList)m.getMusicgroupArtistCollection();
            
      //      itr=a.getSongCollection().iterator();
            int tableCounter=0;
            while (itr.hasNext()){
                Song s1=(Song)itr.next();
                this.songs.add(s1);
                
                data[tableCounter][0]=s1.getTitle();
                data[tableCounter][1]=s1.getTracknr();
                data[tableCounter][2]=s1.getDuration();
                tableCounter++;
            }*/
        }
         else{//αν το album είναι καινούργιο
            this.a=new Album();
          //  this.a.setSongCollection(new ArrayList());
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSave = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        txtTitle = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        cmbArtists = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cmbMusicCompany = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        cmbType = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        cmbDiskNum = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnSave.setText("Αποθήκευση");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        txtTitle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTitleActionPerformed(evt);
            }
        });

        jLabel1.setText("Τίτλος");

        jLabel2.setText("Καλλιτέχνης");

        jLabel3.setText("Εταιρεία");

        jLabel4.setText("Ημερομηνία");

        jLabel5.setText("Τύπος");

        cmbType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "cd single (cs)", "extended_play (ep)", "long_play (lp)", "διπλό long_play (lp)", "τριπλό long_play (lp)", "τετραπλό long_play (lp)" }));

        jLabel6.setText("Αριθμός Δίσκου");

        cmbDiskNum.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbArtists, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbMusicCompany, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(42, 42, 42)
                        .addComponent(cmbDiskNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(31, 31, 31)
                        .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel4))
                .addContainerGap(195, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbArtists, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cmbMusicCompany, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cmbDiskNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22))
        );

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(btnSave)
                        .addGap(45, 45, 45)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(jButton1))
                .addGap(34, 34, 34))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtTitleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTitleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTitleActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        if (!DBManager.getEm().getTransaction().isActive()){
            DBManager.getEm().getTransaction().begin();
        }
        try{
            if(this.mod){
            Album a2= DBManager.getEm().createNamedQuery("Album.findByAlbumId", Album.class).setParameter("albumId", this.a.getAlbumId()).getSingleResult();
            a2.setTitle(txtTitle.getText());
            a2.setFkArtistId((Artist)cmbArtists.getSelectedItem());            
            a2.setType((String)cmbType.getSelectedItem());        
            a2.setDisknumber((int)cmbDiskNum.getSelectedItem());
            a2.setFkMusiccompanyid((Musicproductioncompany)cmbMusicCompany.getSelectedItem());
            //a2.setReleasedate(jDateChooser1.getDate());
            }
            this.a.setTitle(txtTitle.getText());
            this.a.setFkArtistId((Artist)cmbArtists.getSelectedItem());            
            this.a.setType((String)cmbType.getSelectedItem());                    
            this.a.setDisknumber((int)cmbDiskNum.getSelectedItem());
            this.a.setFkMusiccompanyid((Musicproductioncompany)cmbMusicCompany.getSelectedItem());
            //this.a.setReleasedate(jDateChooser1.getDate());
            //έλεγχος για συμπλήρωση υποχρεωτικών πεδίων
       //     if (!this.a.getTitle().equals("") && a.getSongCollection().size()>=1 ){
        
  
       //     System.out.print(a2.getAlbumId());
            if(!this.mod){
                DBManager.getEm().persist(this.a);//προσθήκη στη transaction με τη βάση
            }
                DBManager.getEm().getTransaction().commit();//πραγματοποίηση αλλαγών στη βάση
      //          DBManager.rm.artistalbums=new ArtistAlbums(parent);//άνοιγμα προηγούμενης φόρμας
      //          parent.rm.artistalbums.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
        //        parent.rm.artistalbums.setVisible(true);
                this.dispose();//kleisimo formas
    //        }
    //        else{
   //              JOptionPane.showMessageDialog(null, "Συμπληρώστε ονομα και δώστε τουλάχιστον 2 τραγούδια", "Λάθος", JOptionPane.ERROR_MESSAGE);
    //        }
        } catch (Exception e) {
            e.printStackTrace();
            DBManager.getEm().getTransaction().rollback();
        } 
    }//GEN-LAST:event_btnSaveActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (!DBManager.getEm().getTransaction().isActive()){
        DBManager.getEm().getTransaction().begin();
        }
        try{
            System.out.print(this.a.getAlbumId());
            Album a2= DBManager.getEm().createNamedQuery("Album.findByAlbumId", Album.class).setParameter("albumId", this.a.getAlbumId()).getSingleResult();
            a2.setTitle(txtTitle.getText());
            a2.setFkArtistId((Artist)cmbArtists.getSelectedItem());            
            a2.setType((String)cmbType.getSelectedItem());        
            a2.setDisknumber((int)cmbDiskNum.getSelectedItem());
            a2.setFkMusiccompanyid((Musicproductioncompany)cmbMusicCompany.getSelectedItem());
            //a2.setReleasedate(jDateChooser1.getDate());
            //έλεγχος για συμπλήρωση υποχρεωτικών πεδίων
       //     if (!this.a.getTitle().equals("") && a.getSongCollection().size()>=1 ){
        
            
        //    a2=this.a;
       //     System.out.print(a2.getAlbumId());
       //     DBManager.getEm().persist(a2);//προσθήκη στη transaction με τη βάση

                DBManager.getEm().getTransaction().commit();//πραγματοποίηση αλλαγών στη βάση
      //          DBManager.rm.artistalbums=new ArtistAlbums(parent);//άνοιγμα προηγούμενης φόρμας
      //          parent.rm.artistalbums.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
        //        parent.rm.artistalbums.setVisible(true);
                this.dispose();//kleisimo formas
    //        }
    //        else{
   //              JOptionPane.showMessageDialog(null, "Συμπληρώστε ονομα και δώστε τουλάχιστον 2 τραγούδια", "Λάθος", JOptionPane.ERROR_MESSAGE);
    //        }
        } catch (Exception e) {
           
            DBManager.getEm().getTransaction().rollback();
        } 
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cmbArtists;
    private javax.swing.JComboBox cmbDiskNum;
    private javax.swing.JComboBox cmbMusicCompany;
    private javax.swing.JComboBox cmbType;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtTitle;
    // End of variables declaration//GEN-END:variables
}
